<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-calendar"></i> 员工管理 </el-breadcrumb-item>
                <el-breadcrumb-item> 员工查询 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-collapse-transition>
                    <el-card shadow="never" class="searchCard" v-show="disableBox">
                        <el-form class="form">
                            <el-row>
                                <el-col :span="4">
                                    <el-form-item label="状态">
                                        <el-select v-model="query.status" placeholder="状态" class="handle-select mr10">
                                            <el-option key="1" label="全部" value=""></el-option>
                                            <el-option key="2" label="正常" value="01"></el-option>
                                            <el-option key="3" label="离职" value="00"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <el-form-item label="性别">
                                        <el-select v-model="query.sex" placeholder="性别" class="handle-select mr10">
                                            <el-option key="1" label="全部" value=""></el-option>
                                            <el-option key="2" label="男性" value="01"></el-option>
                                            <el-option key="3" label="女性" value="00"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <el-form-item label="学位">
                                        <el-select v-model="query.education" placeholder="学位" class="handle-select mr10">
                                            <el-option key="00" label="全部" value=""></el-option>
                                            <el-option
                                                v-for="(item, index) in educations"
                                                :key="index"
                                                :label="item"
                                                :value="item"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <el-form-item label="职位">
                                        <el-select v-model="query.pid" placeholder="职位" class="handle-select mr10">
                                            <el-option key="00" label="全部" value=""></el-option>
                                            <el-option
                                                :key="index"
                                                v-for="(item, index) in positions"
                                                :label="item.name"
                                                :value="item.id"
                                            ></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <el-form-item label="姓名">
                                        <el-input v-model="query.name" placeholder="姓名" style="width: 50%"></el-input>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="4">
                                    <el-form-item label="编号">
                                        <el-input v-model="query.tellerId" placeholder="员工编号" style="width: 50%"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row type="flex" justify="center">
                                <el-col :span="2">
                                    <el-form-item>
                                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="2">
                                    <el-form-item>
                                        <el-button type="warning" icon="el-icon-refresh-right" @click="reset">重置</el-button>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </el-card>
                </el-collapse-transition>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-button type="text" @click="searchBox">搜索框</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="tellerId" label="员工编号" min-width="120"></el-table-column>
                <el-table-column prop="name" label="姓名" min-width="120"></el-table-column>
                <el-table-column prop="sex" label="性别" :formatter="formatSex" min-width="60"></el-table-column>
                <el-table-column prop="age" label="年龄" min-width="60"></el-table-column>
                <el-table-column prop="pid" label="职位" :formatter="formatPosition" min-width="120"></el-table-column>
                <el-table-column prop="education" label="学历" min-width="60"></el-table-column>
                <el-table-column prop="phone" label="联系方式" min-width="120"></el-table-column>
                <el-table-column prop="ustatus" label="用户情况" :formatter="formatUStatus" min-width="80"></el-table-column>
                <el-table-column label="状态" min-width="150">
                    <template slot-scope="scope">
                        <el-switch
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            :value="scope.row.status"
                            :active-value="'01'"
                            active-text="正常"
                            inactive-text="离职"
                        ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" min-width="350">
                    <template slot-scope="scope">
                        <el-button type="success" icon="el-icon-view" @click="view(scope.row.id)">详情</el-button>
                        <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row.id)">编辑</el-button>
                        <el-button
                            v-if="scope.row.ustatus != '01'"
                            type="warning"
                            icon="el-icon-circle-plus-outline"
                            @click="generate(scope.row.tellerId)"
                            >开通</el-button
                        >
                        <el-button type="danger" icon="el-icon-close" @click="del(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { fetchData } from '../../../api/index';
export default {
    name: 'basetable',
    inject: ['reload'],
    data() {
        return {
            disableBox: false,
            query: {
                status: '',
                name: '',
                sex: '',
                tellerId: '',
                education: '',
                pageNum: 1,
                pageSize: 5
            },
            educations: [],
            positions: [],
            tableData: [],
            multipleSelection: [],
            delmap: {
                delList: [],
                delTids: []
            },
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getEducations();
        this.getPositions();
        this.init();
    },
    methods: {
        //生成员工用户
        generate(tellerId) {
            this.$confirm('将会使用员工号作为该用户名,密码将随机生成后返回,您确定继续吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.get('/generate/' + tellerId);
                    if (d.code == 200) {
                        this.$alert('密码:' + d.message + ',请尽快更改密码! ', '开通成功', {
                            confirmButtonText: '确定'
                        });
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消开通'
                    });
                });
        },

        //获取学历
        getEducations() {
            this.educations = JSON.parse(window.sessionStorage.getItem('education'));
        },

        // 搜索框
        searchBox() {
            this.disableBox = !this.disableBox;
        },

        // 格式化性别列表
        formatSex(row, column, cellValue, index) {
            if (cellValue == '01') {
                return '男生';
            } else {
                return '女生';
            }
        },

        // 格式化职位列表
        formatPosition(row, column, cellValue, index) {
            if (cellValue == '00') {
                return '暂无职位';
            }
            for (var i = 0; i < this.positions.length; i++) {
                if (this.positions[i].id == cellValue) {
                    return this.positions[i].name;
                }
            }
        },

        // 格式化用户状态列
        formatUStatus(row, column, cellValue, index) {
            if (cellValue == '00') {
                return '未开通';
            } else {
                if (cellValue == '01') {
                    return '已开通';
                } else {
                    return '已删除';
                }
            }
        },

        // 获取
        async init() {
            var d = await this.$http.post('/employees', this.query);
            if (d.code == 200) {
                if (d.data != null) {
                    this.tableData = d.data.list;
                    this.pageTotal = d.data.total;
                }
            } else {
                this.$message.error(d.message);
            }
        },

        // 获取职位
        async getPositions() {
            var condition = {
                did: '',
                status: '01'
            };
            var d = await this.$http.post('/positionMap', condition);
            if (d.code == 200) {
                this.positions = d.data;
            }
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.init();
        },
        // 编辑操作
        view(id) {
            this.$router.push({ path: '/getEmployee', query: { id: id } });
        },
        // 编辑操作
        edit(id) {
            this.$router.push({ path: '/updateEmployee', query: { id: id } });
        },
        // 删除操作
        del(id) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.delete('/employee/' + id);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        async delAllSelection() {
            const length = this.multipleSelection.length;
            var delName = '';
            for (let i = 0; i < length; i++) {
                this.delmap.delList.push(this.multipleSelection[i].id);
                this.delmap.delTids.push(this.multipleSelection[i].tellerId);
                delName = delName + this.multipleSelection[i].name + ' ';
            }
            this.$confirm('确定要删除 ' + delName + '吗？', '提示', {
                type: 'warning'
            })
                .then(async () => {
                    var d = await this.$http.post('/delemployees', this.delmap);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.multipleSelection = [];
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                })
                .catch(() => {});
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageNum = val;
            this.init();
        },
        reset() {
            this.query = {
                status: '',
                name: '',
                sex: '',
                tellerId: '',
                education: '',
                pageNum: 1,
                pageSize: 5
            };
        }
    }
};
</script>

<style scoped>
.searchCard {
    margin-bottom: 20px;
}
.form {
    width: 100%;
    margin: 0 auto;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
